package ar.edu.itba.it.paw.domain;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Product extends PersistentAttributes {
	
	private String name;
	private String description;
	private Date release_date;
	@ManyToOne
	private Category category;
	protected float price;
	private int quantity;
	private boolean state;
	@Column(nullable=false, columnDefinition="integer Default 0")
	private int visits;
	@ManyToOne
	private User owner;
	@OneToMany(cascade=CascadeType.ALL, mappedBy = "product")
	private Set<Transaction> transactions;
	@OneToMany(cascade=CascadeType.ALL, mappedBy = "product")
	private Set<Question> questions;

	
	Product(){
	}
	
	//This constructor is used to create products before they are inserted to the database.
	public Product( String name, String description, Date release_date ,Category category, float price, int quantity, boolean state, User owner){
		if(!(price < 0 || quantity < 0 ) && release_date != null && owner != null){
			this.setName(name);
			this.setDescription(description);
			this.release_date = release_date;
			this.setCategory(category);
			this.price = price;
			this.quantity = quantity;
			this.setState(state);
			this.owner = owner;
			this.visits = 0;
		}else{
			throw new IllegalArgumentException("Invalid parameters");
		}
	}

	public void addTransaction(Transaction trans){
		transactions.add(trans);
	}
	
	public User getOwner() {
		return owner;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if(name != null && !name.equals(""))
			this.name = name;
		else
			throw new IllegalArgumentException("Illegal name");
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		if(description != null && !description.equals(""))
			this.description = description;
		else
			throw new IllegalArgumentException("Illegal description");
	}

	public Date getRelease_date() {
		return release_date;
	}


	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		if(category != null)
			this.category = category;
		else
			throw new IllegalArgumentException("Illegal Category");
	}

	public float getPrice() {
		return price;
	}

	public int getQuantity() {
		return quantity;
	}
	
	public void updateQuantity(int amountBought){
		setQuantity(getQuantity()-amountBought);
	}

	private void setQuantity(int quantity){
		if(quantity < 0)
			throw new IllegalArgumentException("Invalid Quantity");
		else
			this.quantity = quantity;
	}
	
	public boolean getState() {
		return state;
	}

	public void setState(boolean state) {
		this.state = state;
	}

	public Set<Transaction> getTransactions() {
		return transactions;
	}

	public int getVisits() {
		return visits;
	}

	public void visit(){
		visits ++;
	}

	public Set<Question> getQuestions() {
		return questions;
	}
	
	@Override
	public String toString() {
		return "Product [name=" + name + ", description=" + description
				+ ", release_date=" + release_date + ", category=" + category
				+ ", price=" + price + ", quantity=" + quantity + ", state="
				+ state + ", visits=" + visits + ", owner=" + owner + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
		+ ((release_date == null) ? 0 : release_date.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Product other = (Product) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (release_date == null) {
			if (other.release_date != null)
				return false;
		} else if (!release_date.equals(other.release_date))
			return false;
		return true;
	}
}